package org.example.auth.controller;

import org.example.model.system.SysRole;
import org.example.vo.system.AssginRoleVo;
import org.example.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.common.exception.ZspException;
import org.example.result.Result;
import org.example.auth.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags="角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
//    本地访问 http://localhost:8800/admin/system/sysRole/findAll


//    注入serivce
    @Autowired
    SysRoleService sysRoleService;



    //1查询所有的角色
    @ApiOperation("获取角色")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable long userId){
     Map<String, Object> map= sysRoleService.findRoleService(userId);
     return Result.ok(map);
    }


//    2为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo){
        sysRoleService.doAssign(assginRoleVo);
        return  Result.ok();
    }



//    统一返回数据结果
//    http://localhost:8800/doc.html
@ApiOperation("查询所有的角色")
@GetMapping("/findAll")
    public Result findAll(){
//        调用serivce的方法
        List<SysRole> list=sysRoleService.list();
//  模拟异常
    try {
    }catch (Exception e){
        throw new ZspException(200001,"执行了自定义的异常处理");
    }
        return Result.ok(list);

    }

//   1： 条件分页查询,page当前页面 limit每页显示的条数
@PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page,
                                @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo){
//    调用service方法
//        创建page对象，
        Page<SysRole> pageParm=new Page<>(page,limit);
//2：封装条件 判断是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper=new LambdaQueryWrapper<>();
            String roleName= sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName))
        {//封装
            wrapper.like(SysRole::getRoleName,roleName);
        }
        //3调用方法实现
    IPage<SysRole> pagemode1=sysRoleService.page(pageParm,wrapper);

    return Result.ok(pagemode1);
    }

//    添加角色
@PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping ("save")
    public  Result save(@RequestBody SysRole sysRole){
     boolean is_success=sysRoleService.save(sysRole);
     if (is_success){
         return Result.ok();
     }else {
         return Result.fail();
     }
    }

    //修改角色 根据id查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
    SysRole sysRole=sysRoleService.getById(id);
    return Result.ok(sysRole);
    }
    //修改角色，最终修改
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PutMapping ("update")
    public  Result update(@RequestBody SysRole sysRole){
        boolean is_success=sysRoleService.updateById(sysRole);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

//    根据id删除
@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    public Result romove(@PathVariable long id){
     boolean is_success=sysRoleService.removeById(id);
        if (is_success){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
//    批量删除
@PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public  Result batcgRemove(@RequestBody List<Long> idList){

       boolean is_success=sysRoleService.removeByIds(idList);
       if (is_success){
        return  Result.ok();
       }
       else {
       return  Result.fail();
       }

}

}
